<?php
	
	
	echo "<h2>Installation der MySQL Tabellen:</h2>";
	
	$error = false;
	
	$con = @mysql_connect($_SESSION['sql_post']['sql_host'], $_SESSION['sql_post']['sql_user'], $_SESSION['sql_post']['sql_pass']);
	if (!$con) {
		echo '<p class="red">mysql_connect: '.mysql_error().'</p>';
		$error = true;
	}
		
	if($error === false)
	{
		$db_selected = @mysql_select_db($_SESSION['sql_post']['sql_db'], $con);
		if (!$db_selected) {
		  echo '<p class="red">mysql_select_db: '.mysql_error().'</p>';
		  $error = true;
		}
	}
	
	if($error === false)
	{
		$sql_txt = file_get_contents('../sql/install.sql');
		$_sql_arr = explode(";\n", $sql_txt);
		
		foreach($_sql_arr as $k => $v)
		{
			$v = trim($v);
			
			/* finde tabelle name */
			
			preg_match("/\`([a-zA-Z0-9\_]+)\`/", $v, $res);
			
			$table = $res[1];
			
			$q = mysql_query($v);
			
			if($q === false)
			{
				echo '<p class="red">mysql_select_db: '.mysql_error().'</p>'; 
				$error = true; continue;
			} else {
				echo '<p class="green">'.$table.' erfolgreich ausgef&uuml;hrt</p>';
				
			}
		}
		
		/* insert user server informations */
		/* TODO: ADD */
		$sql = "INSERT IGNORE INTO admin_user (id, mail, pass, firstname, lastname, admin) VALUES (1, '{$_SESSION['site_config_post']['UserMail']}', '".md5($_SESSION['site_config_post']['UserPass'])."', '{$_SESSION['site_config_post']['UserFirstname']}', '{$_SESSION['site_config_post']['UserLastname']}', 1)";
		$q = mysql_query($sql);
		mysql_close($con);
	}
	
	if($error === false)
	{
		echo '<input type="button" onclick="location.href=\'?step=5\'" value="Weiter zu Schritt 5" />';
	} else {
		echo '<p class="info">Entfernen Sie alle Tabellen aus der Datenbank "'.$_SESSION['sql_post']['sql_db'].'" und f&uuml;hren Sie diesen Schritt nochmals aus. Schauen Sie sich ggf. das File install.sql einmal manuell an und pr&uuml;fen ob es vorhanden ist.</p>';
		echo '<input type="button" onclick="location.href=\'?step=4\'" value="Neu Laden" />';
	}
	
	
	
?>